<?php
class MessageAction extends Action {
	
	public function queryUser() {
		
		import ( "@.ORG.Page" );
		$Name = $_GET ["username"];
		$Dao = M ( "User" );
		
		// 查询数据
		$condition ['nickname'] = array ('like', '%' . $Name . '%' );
		
		$count = $Dao->where ( $condition )->count ();
		
		$p = new Page ( $count, 10 );
		foreach ( $condition as $key => $val ) {
			$p->parameter .= "$key=" . urlencode ( $val ) . "&";
		}
		// 分页显示输出
		

		$page = $p->show ();
		
		// 当前页数据查询
		

		$list = $Dao->where ( $condition )->order ( 'username ASC' )->limit ( $p->firstRow . ',' . $p->listRows )->select ();
		
		$this->assign ( 'page', $page );
		$this->assign ( 'list', $list );
		
		// 输出模板
		$this->display ();
	}
	
	public function checkName() {
		$q = strtolower ( $_GET ["term"] );
		if (! $q)
			return;
		$Dao = M ( "User" );
		
		$items = $Dao->getField ( "uid,nickname" );
		
		$result = array ();
		foreach ( $items as $id => $key ) {
			if (strpos ( strtolower ( $key ), $q ) !== false) {
				array_push ( $result, array ("value" => $key ) );
			}
			if (count ( $result ) > 11)
				break;
		}
		echo json_encode ( $result );
	}
	public function send() {
		header ( "Content-Type:text/html; charset=utf-8" );
		
		$Dao = D ( "Mail" );
		
		if ($Dao->create ()) {
			$Dao->sendname = session ( 'nickname' );
			if ($lastInsId = $Dao->add ()) {
				$this->success ( "发送成功！" );
			} else {
				$this->error ( "发送错误" );
			}
		} else {
			$this->error ( $Dao->getError () );
		}
	}
	public function sendformajax() {
		$defultmail ['getname'] = $_GET ['getname'];
		if ($_GET ['title'])
			$defultmail ['title'] = "Re: " . $_GET ['title'];
		
		$this->assign ( 'defultmail', $defultmail );
		$this->display ();
	}
	public function sendajax() {
		$Dao = D ( "Mail" );
		if ($vo = $Dao->create ()) {
			$Dao->sendname = session ( 'nickname' );
			if (false !== $Dao->add ()) {
				$this->ajaxReturn ( $vo, '消息发送成功！', 1 );
			} else {
				$this->error ( '消息发送失败！' );
			}
		} else {
			$this->error ( $Dao->getError () );
		}
	
	}
	
	public function readall() {
		
		import ( "@.ORG.Page" );
		$getname = session ( 'nickname' );
		$sendname = $_GET ["sendname"];
		
		$Dao = M ( "Mail" );
		
		// 查询数据
		$condition ['getname'] = $getname;
		$condition ['sendname'] = array ('like', '%' . $sendname . '%' );
		
		$count = $Dao->where ( $condition )->count ();
		
		$p = new Page ( $count, 10 );
		
		foreach ( $condition as $key => $val ) {
			$p->parameter .= "$key=" . urlencode ( $val ) . "&";
		}
		// 分页显示输出
		$p->setConfig ( 'header', '条信息' );
		$page1 = $p->show ();
		
		// 当前页数据查询
		

		$maillist = $Dao->where ( $condition )->order ( 'senddate ASC' )->limit ( $p->firstRow . ',' . $p->listRows )->select ();
		
		$this->assign ( 'page1', $page1 );
		$this->assign ( 'maillist', $maillist );
		
		$condition ['state'] = 0;
		
		$count = $Dao->where ( $condition )->count ();
		
		$p = new Page ( $count, 10 );
		
		foreach ( $condition as $key => $val ) {
			$p->parameter .= "$key=" . urlencode ( $val ) . "&";
		}
		// 分页显示输出
		$p->setConfig ( 'header', '条信息' );
		$page2 = $p->show ();
		
		$maillist = $Dao->where ( $condition )->order ( 'senddate ASC' )->limit ( $p->firstRow . ',' . $p->listRows )->select ();
		
		$this->assign ( 'page2', $page2 );
		$this->assign ( 'unreadmaillist', $maillist );
		// 输出模板
		$this->display ();
	
	}
	public function read() {
		$mid = $_GET ['mid'];
		$getname = session ( 'nickname' );
		$Dao = D ( "Mail" );
		
		$condition ['mid'] = $mid;
		$condition ['getname'] = $getname;
		$vo = $Dao->where ( $condition )->find ();
		$vo ['state'] = 1; //更改信件已读状态
		$result = $Dao->save ( $vo );
		if ($result !== false) {
			$this->assign ( 'mailinfo', $vo );
			$this->display ();
		
		} else {
			
			$this->error ( $Dao->getError () );
		}
	
	}
	
}